<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-form-item label="域名" prop="domain">
        <el-input type="textarea"  :rows="5" v-model="dataForm.domain" placeholder="域名是以主域名为主,支持泛解析,如baidu.com：每条域名后回车输入"></el-input>
      </el-form-item>
      <el-form-item label="域名状态" prop="status">
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option
            v-for="item in statuss"
            :key="item.status"
            :label="item.label"
            :value="item.status">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        statuss: [{
          status: 0,
          label: '关闭'
        }, {
          status: 1,
          label: '正常'
        }],
        dataForm: {
          id: 0,
          domain: '',
          status: '',
          createUserId: '',
          createTime: ''
        },
        dataRule: {
          domain: [
            { required: true, message: '域名不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '域名状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.dataForm.status = 1
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.basedomain.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.domain = data.basedomain.domain
                this.dataForm.status = data.basedomain.status
                this.dataForm.createUserId = data.basedomain.createUserId
                this.dataForm.createTime = data.basedomain.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'domain': this.dataForm.domain,
              'status': this.dataForm.status,
              'createUserId': this.dataForm.createUserId,
              'createTime': this.dataForm.createTime
            }
            var tick = !this.dataForm.id ? API.basedomain.add(params) : API.basedomain.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
